var express = require('express');
var router = express.Router();
var Posts = require('../models/post');
var Userinformation = require('../models/userinformation')
/* post users listing. */
router.post('/list', function(req, res, next) {

    var count=0;
    var page=req.body.page;
    var rows=req.body.rows;

    var username=req.body.username;
    console.log(username);
    console.log("page:"+page+",rows:"+rows);

    // var query=Posts.find({});
    // query.skip((page-1)*rows);
    // query.limit(rows*1);
    // if(username){
    //     query.where('username',qs);
    // }
    // query.where('isdelete','0');

    var query = {};
    // if(username){
    //     query.username = username;
    // }

    var qs=new RegExp(username);

    query.isdelete = '0';
    //计算分页数据
    Posts.find(query).
    populate({path:'user', select: 'username',match: {username:qs}}).
    skip((page-1)*rows).
    limit(rows*1).exec(function(err,rs){
        if(err){
            var dataError = {
                status: '0',
                msg: err.message
            };
            res.end(JSON.stringify(dataError));
        }

        else {

            var resaults = [];
            if (rs.length>0){
                for(var i = 0;i<rs.length;i++){
                    var obj = rs[i];
                    var user = obj['user'];
                    if(user != null){
                        resaults.push(obj);
                    }
                }
            }

            var sumPage = ((rs.length%rows)==0)?(rs.length/rows):(Math.floor(rs.length/rows)+1);
            var dataSuccess = {
                status: '200',
                data:resaults,
                sumpage:sumPage,
                msg: '查询成功'
            };
            res.end(JSON.stringify(dataSuccess));
        }
    });

});





//删除用户
router.post('/delete',function(req,res,next) {
    var body = req.body;
    console.log(body);
    if(body){
        var postid  = body.postid;
        Posts.findOne({_id:postid},function (err,postinfo) {
            if(err){
                var dataError = {
                    status: '0',
                    msg: err.message
                };
                res.end(JSON.stringify(dataError));
            }
            else if(postinfo){


                var whereStr = {"_id":postid};
                var updateStr = {$set: { "isdelete" : '1' }};

                Posts.update(whereStr,updateStr,function () {
                    if(err){
                        var dataError = {
                            status: '0',
                            msg: err.message
                        };
                        res.end(JSON.stringify(dataError));
                    }
                    else {
                        var dataSuccess = {
                            status: '200',
                            msg: '删除成功'
                        };
                        res.end(JSON.stringify(dataSuccess));
                    }

                });


            }
            else {
                var dataError = {
                    status: '0',
                    msg: '动态不存在'
                };
                res.end(JSON.stringify(dataError));
            }
        });


    }


})


module.exports = router;

